<template>
  <div id="app">

    <div style="width: 500px;text-align: left;margin-left: 80px">
      <el-image :src="image" style="width: 450px; height: 550px;"
                :preview-src-list="srcList"></el-image>
      <el-upload
          class="upload-demo"
          :show-file-list="false"
          accept="image/*"
          action=""
          :http-request="postResumeImage"
          :before-upload="beforeAvatarUpload">
        <el-button size="small" type="primary">点击上传</el-button>
        <div slot="tip" class="el-upload__tip">只能上传图片文件，且不超过2MB</div>
      </el-upload>

    </div>
  </div>
</template>

<script>

import {getSeekerDetail, getImage, postImage} from "@/api";
import jwtUtil from "jwt-decode";

export default {
  name: "seeResumeInformation",
  data() {
    return {
      srcList: [''],
      image: '',
      user: {
        userId: '',
        objectId: '',
        userType: ''
      },
      seeker: {
        id: '',
        seekerName: '',
        resume: ''
      },
    }
  },
  methods: {
    async init() {
      this.user = jwtUtil(window.localStorage.getItem('Token'))
      await getSeekerDetail(this.$route.query.id).then(res => {
        this.seeker.id = res.data.id
        this.seeker.seekerName = res.data.seekerName
        this.seeker.resume = res.data.resume
      })
      getImage({
            objectId: this.user.objectId,
            userType: this.user.userType,
            fileName: this.seeker.resume,
            fileType: 2
          }
      ).then(res => {
        this.image = window.URL.createObjectURL(res.data);
        this.srcList[0] = window.URL.createObjectURL(res.data);
      })
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    postResumeImage(params) {
      const formData = new FormData();
      const file = params.file;
      formData.append("file", file);
      formData.append('objectId', this.user.objectId)
      formData.append('userType', this.user.userType)
      formData.append('fileType', 2)
      postImage(formData).then(res => {
        this.$message.success('上传成功！！！')
        this.init()
        return res
      })
    }
  },
  created() {
    this.init()
  }
}
</script>

<style scoped>


</style>